package com.bootdo.shop.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.shop.domain.ProductCategoryDO;
import com.bootdo.shop.service.ProductCategoryService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 商品分类关联表
 * 
 * @author yhg
 * @email 411741962@qq.com
 * @date 2017-12-15 14:06:51
 */
 
@Controller
@RequestMapping("/shop/productCategory")
public class ProductCategoryController {
	@Autowired
	private ProductCategoryService productCategoryService;
	
	@GetMapping()
	@RequiresPermissions("shop:productCategory:productCategory")
	String ProductCategory(){
	    return "shop/productCategory/productCategory";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("shop:productCategory:productCategory")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<ProductCategoryDO> productCategoryList = productCategoryService.list(query);
		int total = productCategoryService.count(query);
		PageUtils pageUtils = new PageUtils(productCategoryList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("shop:productCategory:add")
	String add(){
	    return "shop/productCategory/add";
	}

	@GetMapping("/edit/{productCategoryId}")
	@RequiresPermissions("shop:productCategory:edit")
	String edit(@PathVariable("productCategoryId") Long productCategoryId,Model model){
		ProductCategoryDO productCategory = productCategoryService.get(productCategoryId);
		model.addAttribute("productCategory", productCategory);
	    return "shop/productCategory/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("shop:productCategory:add")
	public R save( ProductCategoryDO productCategory){
		if(productCategoryService.save(productCategory)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("shop:productCategory:edit")
	public R update( ProductCategoryDO productCategory){
		productCategoryService.update(productCategory);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("shop:productCategory:remove")
	public R remove( Long productCategoryId){
		if(productCategoryService.remove(productCategoryId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("shop:productCategory:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] productCategoryIds){
		productCategoryService.batchRemove(productCategoryIds);
		return R.ok();
	}
	
}
